$(document).ready(function() {
	$(".listuser tbody tr td .edit").click(function(event) {
		event.preventDefault();
		var tr = $(event.target).parent().parent();
		$("input[name='id']").val($(tr).find("#id").text());
		$("input[name='name']").val($(tr).find("#name").text());
		$(".img").load("edit?img="+$(tr).find("#avatar").text());
		$("input[name='age']").val($(tr).find("#age").val());
		$("input[name='xmap']").val($(tr).find("#xmap").val());
		$("input[name='ymap']").val($(tr).find("#ymap").val());
		$("input[name='vocation']").val($(tr).find("#vocation").val());
		$("textarea[name='address']").text($(tr).find("#address").val());
		$("input[name='phonenumber']").val($(tr).find("#phonenumber").val());
		$("textarea[name='notes']").text($(tr).find("#notes").val());
		var action = $("#user").attr("action");
		$("#user").attr("action",action.replace("add","edit"));
	});
	
	


	$(".listuser tbody tr td .del").click(function(event) {

		$.ajax({
			url : $(event.target).attr("href"),
			type : "DELETE",

			beforeSend : function(xhr) {
				xhr.setRequestHeader("Accept", "application/json");
				xhr.setRequestHeader("Content-Type", "application/json");
			},

			success : function(smartphone) {
				// var respContent = "";
				var rowToDelete = $(event.target).closest("tr");

				rowToDelete.remove();

				// respContent += "<span class='success'>Smartphone was deleted:
				// [";
				// respContent += smartphone.producer + " : ";
				// respContent += smartphone.model + " : " ;
				// respContent += smartphone.price + "]</span>";

				// $("#sPhoneFromResponse").html(respContent);
			}

		});
		event.preventDefault();
	});
	$("input[name='file']").change(function(event) {
		// alert($(this).val());
		var obj = $(this);
		var data = new FormData();
		$.each($(obj)[0].files, function(i, file) {
			data.append('avatar', file);
		});
		$.ajax({
			url : 'upload',
			data : data,
			cache : false,
			contentType : false,
			processData : false,
			type : 'POST',
			success : function(data) {
				//alert(data);
				$(".img").html(data);
				
			}
		});
	});
	
	$(".taskbar #btnadd").click(function(){
		$("#user")[0].reset();
		$("textarea").text("");
		$(".img").html("");
		var action = $("#user").attr("action");
		$("#user").attr("action",action.replace("edit","add"));
	});
});